package xps.and.uudaijia.userclient.view.activity;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.jude.utils.JUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Subscriber;
import xps.and.uudaijia.userclient.R;
import xps.and.uudaijia.userclient.data.baen.SmsResponce;
import xps.and.uudaijia.userclient.data.net.UserNetWorks;
import xps.and.uudaijia.userclient.util.CommonUtils;
import xps.and.uudaijia.userclient.util.DownTimer;
import xps.and.uudaijia.userclient.view.base.BaseActivity;

/**
 * Created by Administrator on 2017/7/31.
 */

public class UpPasswordActivity extends BaseActivity {
    @BindView(R.id.actionbar_iv_back)
    ImageView actionbarIvBack;
    @BindView(R.id.et_phoneNum)
    EditText etPhoneNum;
    @BindView(R.id.et_verifyCode)
    EditText etVerifyCode;
    @BindView(R.id.Verification_TextView)
    TextView VerificationTextView;
    @BindView(R.id.XN_phoneNum)
    EditText XNPhoneNum;
    @BindView(R.id.XM_phoneNum)
    EditText XMPhoneNum;
    @BindView(R.id.NextStep_Button)
    Button NextStepButton;
    public static int Interval = 1000;
    DownTimer timer;

    @Override
    protected int getLayout() {
        return R.layout.activity_uppassword;
    }

    @Override
    protected void setData() {

    }

    @Override
    protected void init() {
        ButterKnife.bind(this);
    }

    @OnClick({R.id.actionbar_iv_back, R.id.Verification_TextView, R.id.NextStep_Button})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.actionbar_iv_back:
                finish();
                break;
            case R.id.Verification_TextView:
                if (!CommonUtils.isMobileNumber(etPhoneNum.getText
                        ().toString().trim())) {
                    JUtils.Toast("手机号码格式不合法");
                    return;
                }

                VerificationTextView.setEnabled(false);
                timer = new DownTimer();
                timer.setTotalTime(60 * Interval);
                timer.setIntervalTime(Interval);
                timer.setTimerLiener(new DownTimer.TimeListener() {
                    @Override
                    public void onFinish() {
                        VerificationTextView.setEnabled(true);
                        VerificationTextView.setText("获取验证码");
                    }

                    @Override
                    public void onInterval(long remainTime) {
                        VerificationTextView.setText(remainTime / 1000
                                + "秒后" + "再试");
                    }
                });
                timer.start();
                UPMMYZM();
                break;
            case R.id.NextStep_Button:
                String XNPhoneNumStr = XNPhoneNum.getText().toString();
                String XMPhoneNumStr = XMPhoneNum.getText().toString();
                if (XNPhoneNumStr.equals(XMPhoneNumStr)) {
                    UPMM();
                } else {
                    JUtils.Toast("两次密码不同");
                }
                break;
        }
    }

    void UPMMYZM() {
        UserNetWorks.getUPMMYZM(etPhoneNum.getText().toString(), new Subscriber<SmsResponce>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(SmsResponce smsResponce) {
                if (smsResponce.getReturn_code().equals("SUCCESS")) {
                    JUtils.Toast(smsResponce.getReturn_msg());
                } else if (smsResponce.getReturn_code().equals("FAIL")) {
                    JUtils.Toast(smsResponce.getReturn_msg());
                }
            }
        });
    }

    void UPMM() {
        UserNetWorks.getUPUPMM(etVerifyCode.getText().toString(), etPhoneNum.getText().toString(), XMPhoneNum.getText().toString(), new Subscriber<SmsResponce>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(SmsResponce smsResponce) {
                if (smsResponce.getReturn_code().equals("SUCCESS")) {
                    JUtils.Toast(smsResponce.getReturn_msg());
                    finish();
                } else if (smsResponce.getReturn_code().equals("FAIL")) {
                    JUtils.Toast(smsResponce.getReturn_msg());
                }
            }
        });
    }
}